﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Pixysoft.Framework.Configurations.Core
{
    class ConfigurationPropertyCollection : IConfigurationPropertyCollection
    {
        int index = -1;

        List<IConfigurationProperty> properties = new List<IConfigurationProperty>();

        public IConfigurationProperty Add(string attributename)
        {
            if (string.IsNullOrEmpty(attributename))
                throw new Exception("missing attributename.");

            attributename = attributename.Trim();

            if (Contains(attributename))
                throw new Exception("node attributename already in nodes, attributename = " + attributename);

            ConfigurationProperty property = new ConfigurationProperty(attributename);

            properties.Add(property);

            return property;
        }

        public IConfigurationProperty Add(string attributename, string attributevalue)
        {
            if (string.IsNullOrEmpty(attributename))
                throw new Exception("missing attributename.");

            attributename = attributename.Trim();

            if (Contains(attributename))
                throw new Exception("node attributename already in nodes, attributename = " + attributename);

            ConfigurationProperty property = new ConfigurationProperty(attributename, attributevalue);

            properties.Add(property);

            return property;
        }

        public string this[string propertyName]
        {
            get
            {
                if (string.IsNullOrEmpty(propertyName))
                    throw new Exception("missing input nodeName");

                propertyName = propertyName.Trim();

                foreach (IConfigurationProperty property in properties)
                {
                    if (string.Equals(property.Key, propertyName, StringComparison.OrdinalIgnoreCase))
                        return property.Value;
                }

                throw new Exception("nodes do not contain propertyName = " + propertyName);
            }
        }

        public IConfigurationProperty this[int index]
        {
            get
            {
                if (index < 0 || index > properties.Count - 1)
                    throw new Exception("index exceed properties.count = " + properties.Count);

                return properties[index];
            }
        }

        public bool Contains(string attributename)
        {
            if (string.IsNullOrEmpty(attributename))
                return false;

            foreach (IConfigurationProperty property in properties)
            {
                if (string.Equals(property.Key, attributename, StringComparison.OrdinalIgnoreCase))
                    return true;
            }

            return false;
        }

        public override string ToString()
        {
            return "properties.count = " + properties.Count;
        }

        public int Count
        {
            get
            {
                return properties.Count;
            }
        }

        #region IEnumerable<IConfigurationProperty> 成员

        public IEnumerator<IConfigurationProperty> GetEnumerator()
        {
            return this;
        }

        #endregion

        #region IEnumerable 成员

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this;
        }

        #endregion

        #region IEnumerator<IConfigurationProperty> 成员

        public IConfigurationProperty Current
        {
            get
            {
                return this[index];
            }
        }

        #endregion

        #region IDisposable 成员

        public void Dispose()
        {
            Reset();
        }

        #endregion

        #region IEnumerator 成员

        object System.Collections.IEnumerator.Current
        {
            get
            {
                return this[index];
            }
        }

        public bool MoveNext()
        {
            if (index >= properties.Count - 1)
                return false;

            index++;

            return true;
        }

        public void Reset()
        {
            index = -1;
        }

        #endregion
    }
}
